/**
 ****************************************************************************************
 *
 * @file sleep.c
 *
 * @brief Sleep driver for QN9020.
 *
 * Copyright (C) Quintic 2012-2013
 *
 * $Rev: 1.0 $
 *
 ****************************************************************************************
 */

/**
 ****************************************************************************************
 * @addtogroup  SLEEP
 * @{
 ****************************************************************************************
 */

/*
 * INCLUDE FILES
 ****************************************************************************************
 */

#include "sleep.h"
#if CONFIG_ENABLE_DRIVER_SLEEP==TRUE && CONFIG_ENABLE_ROM_DRIVER_SLEEP==FALSE
#include "syscon.h"
#include "intc.h"
#ifdef BLE_PRJ
#include "lib.h"
#include "usr_design.h"
#include "uart.h"
#include "button.h"
#endif

/*
 * STRUCTURE DEFINITIONS
 ****************************************************************************************
 */

/*
 * GLOBAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */
struct sleep_env_tag sleep_env = {QN_DEEP_SLEEP, 0, true, 0, 0};

/*
 * LOCAL FUNCTION DEFINITIONS
 ****************************************************************************************
 */


/*
 * EXPORTED FUNCTION DEFINITIONS
 ****************************************************************************************
 */
#ifdef BLE_PRJ
/**
 ****************************************************************************************
 * @brief   Check application whether to enter sleep mode
 * @return  sleep allowed status
 ****************************************************************************************
 */
int usr_sleep(void)
{
    int32_t rt;

    rt = sleep_get_pm();

    // If the BLE timer queue is not NULL, prevent entering into DEEPSLEEP mode
    if(rt == PM_DEEP_SLEEP && !ke_timer_empty())
    {
        rt = PM_SLEEP;
    }

    // Check Device status
    if((rt >= PM_SLEEP)
       && dev_get_bf())
    {
        // If any devices are still working, the chip cann't enter into SLEEP/DEEPSLEEP mode.
        rt = PM_IDLE;
    }

    if ((rt >= PM_SLEEP) && (!gpio_sleep_allowed()))
    {
        return PM_ACTIVE;    // If CLOCK OFF & POWER DOWN is disabled, return immediately
    }

#if QN_DBG_PRINT
    int uart_tx_st = uart_check_tx_free(QN_DEBUG_UART);
    
    if((rt >= PM_SLEEP) && (uart_tx_st == UART_TX_BUF_BUSY))
    {
        rt = PM_IDLE;
    }
    else if(uart_tx_st == UART_LAST_BYTE_ONGOING)
    {
        return PM_ACTIVE;    // If CLOCK OFF & POWER DOWN is disabled, return immediately
    }
#endif

#if QN_EACI
    if((GPIO_LOW == gpio_read_pin(QN_EACI_GPIO_WAKEUP_QN_MCU))  // Check external wakeup source
        || (eaci_env.tx_state!=EACI_STATE_TX_IDLE)              // Check EACI UART TX status
        || (eaci_env.rx_state!=EACI_STATE_RX_START))            // Check EACI UART RX status
    {
        rt = PM_IDLE;
    }

    int tx_st = uart_check_tx_free(QN_HCI_UART);

    if((rt >= PM_SLEEP) && (tx_st == UART_TX_BUF_BUSY))
    {
        rt = PM_IDLE;
    }
    else if(tx_st == UART_LAST_BYTE_ONGOING)
    {
        return PM_ACTIVE;    // If CLOCK OFF & POWER DOWN is disabled, return immediately
    }
#endif

    return rt;
}
#endif

/**
 ****************************************************************************************
 * @brief  Init sleep power down modules
 * @description
 *  This function is used to init MCU sleep mode.
 *****************************************************************************************
 */
void sleep_init(void)
{
    // --------------------------------------------
    // sleep
    // --------------------------------------------
    
    //23 : PD_XTAL32
    //10 : PD_RCO
    // 7 : PD_MEM7
    // 6 : PD_MEM6
    // 5 : PD_MEM5
    // 4 : PD_MEM4
    // 3 : PD_MEM3
    // 2 : PD_MEM2
    // 1 : PD_MEM1
    // 0 : PL_VREG_D
    sleep_env.retention_modules |= QN_MEM_RETENTION;

    // power down all module in sleep except retention modules
#if (defined(QN_9020_B2) || defined(QN_9020_B1))
    syscon_SetPGCR0WithMask(QN_SYSCON, 0xF7FFFCFE, (0xFFFFFC00 | QN_MEM_UNRETENTION));
#elif defined(QN_9020_B0)
    syscon_SetPGCR0WithMask(QN_SYSCON, 0xFFFFFCFE, (0xFFFFFC00 | QN_MEM_UNRETENTION));
#endif

    // power down all unretention memory all the time.
    // if you want to use the unretention memory in the active mode, remove the following snippet.
    syscon_SetPGCR1WithMask(QN_SYSCON, 
                            (SYSCON_MASK_DIS_MEM1
                            | SYSCON_MASK_DIS_MEM2
                            | SYSCON_MASK_DIS_MEM3
                            | SYSCON_MASK_DIS_MEM4
                            | SYSCON_MASK_DIS_MEM5
                            | SYSCON_MASK_DIS_MEM6
                            | SYSCON_MASK_DIS_MEM7),
                            QN_MEM_UNRETENTION);
}

/**
 ****************************************************************************************
 * @brief  Enable sleep mode
 * @param[in]    mode           sleep mode
 * @param[in]    iconfig        wakeup interrupt config
 * @param[in]    callback       callback after wakeup
 * @description
 *  This function is used to set MCU into sleep mode, before enter sleep, wakeup source should be set.
 *****************************************************************************************
 */
void enter_sleep(enum SLEEP_MODE mode, uint32_t iconfig, void (*callback)(void))
{
    if (mode == SLEEP_CPU_CLK_OFF) {
        // --------------------------------------------
        // cpu clock disable
        // --------------------------------------------

        // Ensure we use deep SLEEP - SLEEPDEEP should be set
        // SCR[2] = SLEEPDEEP
        SCB->SCR |= (1UL << 2);
        // set pd state to deep gating
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_PMUENABLE, MASK_DISABLE);

        GLOBAL_INT_DISABLE();
#if SLEEP_CONFIG_EN == TRUE
        NVIC->ISER[0] = iconfig;
#endif
        // Wait For Interrupt
        __WFI();  // Enter sleep mode

        // Wakeup when interrupt is triggered
        GLOBAL_INT_RESTORE();

        // TODO
    }
    else if ((mode == SLEEP_NORMAL) || (mode == SLEEP_DEEP)) {
        
#if QN_LOW_POWER_MODE_EN==TRUE
        enter_low_power_mode(0);

        // --------------------------------------------
        // cpu clock disable
        // --------------------------------------------

        // Ensure we use deep SLEEP - SLEEPDEEP should be set
        // SCR[2] = SLEEPDEEP
        SCB->SCR |= (1UL << 2);

#else
        
        // --------------------------------------------
        // sleep or deep sleep
        // --------------------------------------------
#ifdef BLE_PRJ
        // Save configuration before power down
        save_ble_setting();
#endif

#if (defined(QN_9020_B1) && QN_PMU_VOLTAGE)
        // Switch off REF PLL power
        syscon_SetPGCR1WithMask(QN_SYSCON, SYSCON_MASK_DIS_REF_PLL, MASK_ENABLE);
#endif
        
        // switch to internal 20MHz
        syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_CLK_MUX, CLK_INT_20M<<SYSCON_POS_CLK_MUX);

        if(mode == SLEEP_NORMAL)
        {
#if (QN_DEEP_SLEEP_EN)
            sleep_env.deep_sleep = false;
#endif
            // power down all module in sleep except 32K and retention memory
            syscon_SetPGCR0WithMask(QN_SYSCON, sleep_env.retention_modules|0x00000001, 0x00000001);
        }
        else
        {
#if (QN_DEEP_SLEEP_EN)
            sleep_env.deep_sleep = true;
#endif
            // power down all module in deep sleep except retention memory
#if (defined(QN_9020_B2) || defined(QN_9020_B1))
            syscon_SetPGCR0WithMask(QN_SYSCON, 0xF7FFFCFF, 0xFFFFFC01|~sleep_env.retention_modules);
#elif defined(QN_9020_B0)
            syscon_SetPGCR0WithMask(QN_SYSCON, 0xFFFFFCFF, 0xFFFFFC01|~sleep_env.retention_modules);
#endif
        }

#if (defined(QN_9020_B0) && QN_PMU_VOLTAGE)
        syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_AHB_DIV_BYPASS|SYSCON_MASK_AHB_DIVIDER, 
                                              (0xf<<SYSCON_POS_AHB_DIVIDER));
#endif

        // Ensure we use deep SLEEP - SLEEPDEEP should be set
        // SCR[2] = SLEEPDEEP
        SCB->SCR |= (1UL << 2);
        // set pd state to sleep
#if !QN_PMU_VOLTAGE
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_DVDD12_PMU_SET|SYSCON_MASK_PMUENABLE, MASK_ENABLE);
#else
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_PMUENABLE, MASK_ENABLE);
#endif

        syscon_SetIvrefX32WithMask(QN_SYSCON, SYSCON_MASK_VREG12_A|SYSCON_MASK_VREG12_D|SYSCON_MASK_DVDD12_SW_EN, 
                                              (0x0 << SYSCON_POS_VREG12_A)|(0x0 << SYSCON_POS_VREG12_D));

#endif // QN_LOW_POWER_MODE_EN==TRUE

#if SLEEP_CONFIG_EN == TRUE
        NVIC->ICPR[0] = 0x00000020;  // clear OSC_EN pending flag
        NVIC->ISER[0] = iconfig;
#endif
        // Wait For Interrupt
        __WFI();  // Enter sleep mode
        // Wakeup when sleep timer, comparator or gpio is triggered

        // Disable interrupt in the wakeup procedure.
        NVIC->ICER[0] = iconfig;

#if QN_LOW_POWER_MODE_EN==TRUE
        restore_from_low_power_mode(callback);
#else

        // 1.2V
        syscon_SetIvrefX32WithMask(QN_SYSCON, SYSCON_MASK_VREG12_A|SYSCON_MASK_VREG12_D|SYSCON_MASK_DVDD12_SW_EN, 
                                              (0x1 << SYSCON_POS_VREG12_A)|(0x0 << SYSCON_POS_VREG12_D)|SYSCON_MASK_DVDD12_SW_EN);

#if (defined(QN_9020_B0) && QN_PMU_VOLTAGE)
        syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_AHB_DIV_BYPASS, MASK_ENABLE);
#endif
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_DVDD12_PMU_SET, MASK_DISABLE);

#if SLEEP_CALLBACK_EN == TRUE
        if (callback != NULL) {
            callback();
        }
#endif

#if (defined(QN_9020_B0) && QN_PMU_VOLTAGE)
        syscon_set_ahb_clk(__AHB_CLK);
#endif

        // 16MHz/32MHz XTAL is ready
        while (!(syscon_GetBLESR(QN_SYSCON) & SYSCON_MASK_CLK_RDY))
        {
            // XTAL shall be ready before BLE wakeup
            if (*(volatile uint32_t *)(0x2f000014) & 0x4)
            {
                // In this case XTAL wakeup duration is larger than setting.
                // The parameter 'Oscillator wake-up time' in the NVDS should be revised.
#if (QN_DBG_INFO)
                set_dbg_info(QN_DBG_INFO_XTAL_WAKEUP_DURATION);
#endif
            }
        }
        syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_CLK_MUX, CLK_XTAL<<SYSCON_POS_CLK_MUX);

#endif // QN_LOW_POWER_MODE_EN==TRUE

#if ((defined(QN_9020_B2) || defined(QN_9020_B1)) && defined(BLE_PRJ))
        sleep_post_process();
#endif
    }
}

#if GPIO_WAKEUP_EN == TRUE
/**
 ****************************************************************************************
 * @brief  Set GPIO wakeup
 * @param[in]    pin         wakeup pin: P0 and P1
 * @param[in]    type        Wakeup type: high, low, change
 * @description
 *  This function is used to set MCU wakeup by gpio pin.
 *****************************************************************************************
 */
void wakeup_by_gpio(enum gpio_pin pin, enum gpio_wakeup_type type)
{
    if (sleep_env.wakeup_by_sleeptimer == 0) {
        // Disable sleep timer wakeup
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_OSC_WAKEUP_EN, MASK_DISABLE);
    }
    
    // configure gpio wakeup pin
    gpio_wakeup_config(pin, type);
    gpio_enable_interrupt(pin);

    // Ensure gpio interrupt is not pending before the test
    NVIC_ClearPendingIRQ(GPIO_IRQn);
    // Enable Interrupts
    NVIC_EnableIRQ(GPIO_IRQn);
}
#endif

#if ACMP_WAKEUP_EN == TRUE
/**
 ****************************************************************************************
 * @brief  Set analog comparator wakeup
 * @param[in]    acmpch         enum ACMP_CH
 * @param[in]    callback       Callback function pointer, which is called in IRQHandler.
 * @description
 *  This function is used to set MCU wakeup by analog comparator.
 *****************************************************************************************
 */
void wakeup_by_analog_comparator(enum ACMP_CH acmpch, void (*callback)(void))
{
    if (sleep_env.wakeup_by_sleeptimer == 0) {
        // Disable sleep timer wakeup
        syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_OSC_WAKEUP_EN, MASK_DISABLE);
    }
    
    if (acmpch == ACMP0) {
        analog_pin_enable(AIN0, MASK_ENABLE);
    }
    else {
        analog_pin_enable(AIN2, MASK_ENABLE);
    }
    comparator_init(acmpch, VDD_8, ACMPO_0_GEN_INT, callback);
}
#endif

#if SLEEP_TIMER_WAKEUP_EN == TRUE
/**
 ****************************************************************************************
 * @brief  Set sleep timer wakeup
 * @param[in]    clk_src      32KHz clock source
 * @description
 *  This function is used to set MCU wakeup by sleep timer.
 *****************************************************************************************
 */
void wakeup_by_sleep_timer(int clk_src)
{
    uint32_t dis_type, en_type;
    uint32_t pd_type;
    
    // Enable sleep timer wakeup 
    syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_OSC_WAKEUP_EN, MASK_ENABLE);
    sleep_env.wakeup_by_sleeptimer = 1;

    if (clk_src == RCO_32K) {
        // Enable 32k RCO
        dis_type = SYSCON_MASK_DIS_RCO;
        en_type = MASK_ENABLE;
        pd_type = SYSCON_MASK_PD_XTAL32;
        sleep_env.retention_modules |= SYSCON_MASK_DIS_RCO;
    }
    else {
        // Enable 32k XTAL
        dis_type = SYSCON_MASK_DIS_XTAL32;
        en_type = MASK_DISABLE;
        pd_type = SYSCON_MASK_PD_RCO;
        sleep_env.retention_modules |= SYSCON_MASK_DIS_XTAL32;
    }
    syscon_SetPGCR1WithMask(QN_SYSCON, dis_type, MASK_DISABLE);
    syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_SEL_CLK_32K, en_type);
    syscon_SetPGCR0WithMask(QN_SYSCON, SYSCON_MASK_PD_XTAL32|SYSCON_MASK_PD_RCO, pd_type);
}
#endif

#if SLEEP_CALLBACK_EN == TRUE
/**
 ****************************************************************************************
 * @brief  Sleep wakeup callback function
 * @description
 *  This function will be called before clock switching to XTAL in sleep mode.
 *****************************************************************************************
 */
void sleep_cb(void)
{
#if __XTAL == XTAL_32MHz
    // external clock is 32MHz
    syscon_SetLO1WithMask(QN_SYSCON, SYSCON_MASK_XDIV, MASK_ENABLE);
#endif

#ifdef BLE_PRJ    
    // Restore configuration
    restore_ble_setting();

    // Restore peripheral used in the application
    usr_sleep_restore();
#endif    
    
    // clear pending interrupt, except sleep mode wakeup source: analog comparator0/1, gpio, osc_en
    NVIC->ICPR[0] = 0xffffffd8;
}
#endif

#ifdef BLE_PRJ
#if (QN_DEEP_SLEEP_EN && !QN_32K_RCO)
/**
 ****************************************************************************************
 * @brief  Wakeup 32k XTAL
 * @description
 *  This function will be called after waking up from deep sleep mode.
 *****************************************************************************************
 */
void wakeup_32k_xtal_switch_clk(void)
{
    if(sleep_env.deep_sleep)
    {
        // prevent ble sleep
        enable_ble_sleep(false);

        // ensable schmitt trigger in 32.768KHz buffer
        syscon_SetIvrefX32WithMask(QN_SYSCON, SYSCON_MASK_X32SMT_EN, MASK_ENABLE);
        // Set 32.768KHz xtal higher current, that can let the 32k xtal stable fastly (decrease stable time).
        syscon_SetIvrefX32WithMask(QN_SYSCON, SYSCON_MASK_X32ICTRL, 32);
        
        // switch to 32k RCO
        syscon_SetPGCR1WithMask(QN_SYSCON, SYSCON_MASK_DIS_RCO, MASK_DISABLE);
#if (QN_PMU_VOLTAGE)
        delay(10);
#else
        delay(300);
#endif
        syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_SEL_CLK_32K, MASK_ENABLE);
    }
}

/**
 ****************************************************************************************
 * @brief  Wakeup 32k XTAL
 * @description
 *  This function will be called after waking up from deep sleep mode.
 *****************************************************************************************
 */
void wakeup_32k_xtal_start_timer(void)
{
    if(sleep_env.deep_sleep)
    {
        // start 32k XTAL wakeup timer
        ke_timer_set(APP_SYS_32K_XTAL_WAKEUP_TIMER, TASK_APP, 50);
    }
}
#endif
#endif

uint32_t PGCR1_restore;
uint8_t low_power_mode_en = 0;
/**
 ****************************************************************************************
 * @brief  Enter low power mode
 * @param[in]    en       enabled peripheral at low power mode
 * @description
 *  This function is used to set MCU entering into low power mode.
 *****************************************************************************************
 */
void enter_low_power_mode(uint32_t en)
{
    PGCR1_restore = syscon_GetPGCR1(QN_SYSCON);
    syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_DVDD12_PMU_SET, MASK_ENABLE);
    syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_PMUENABLE, MASK_DISABLE);
    
    low_power_mode_en = 1;
    // set system clock to 32K
    syscon_set_sysclk_src(CLK_LOW_32K, __32K_TYPE);
    syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_AHB_DIV_BYPASS, MASK_ENABLE);

    uint32_t mask;
    // power off all not needed modules
    mask = SYSCON_MASK_DIS_OSC
         | SYSCON_MASK_DIS_BG
         | SYSCON_MASK_DIS_V2I
         | SYSCON_MASK_DIS_BUCK
         | SYSCON_MASK_DIS_VREG_A
         | SYSCON_MASK_DIS_VREG_D
         | SYSCON_MASK_DIS_XTAL
#if QN_32K_RCO == TRUE
         | SYSCON_MASK_DIS_XTAL32
#endif
         | SYSCON_MASK_DIS_REF_PLL
         | SYSCON_MASK_DIS_LO_VCO
         | SYSCON_MASK_DIS_LO_PLL
         | SYSCON_MASK_DIS_PA
         | SYSCON_MASK_DIS_LNA
         | SYSCON_MASK_DIS_LNA_PKDET
         | SYSCON_MASK_DIS_MIXER
         | SYSCON_MASK_DIS_PPF_PKDET
         | SYSCON_MASK_DIS_PPF
         | SYSCON_MASK_DIS_RX_PKDET
         | SYSCON_MASK_DIS_RX_ADC
         | SYSCON_MASK_DIS_SAR_ADC
#if (QN_32K_RCO == FALSE)
         | SYSCON_MASK_DIS_RCO
#endif
         | SYSCON_MASK_DIS_MEM7
         | SYSCON_MASK_DIS_MEM6
         | SYSCON_MASK_DIS_MEM5
         | SYSCON_MASK_DIS_MEM4
         | SYSCON_MASK_DIS_MEM3
         | SYSCON_MASK_DIS_MEM2
         | SYSCON_MASK_DIS_MEM1
         | SYSCON_MASK_DIS_SAR_BUF
         ;
    syscon_SetPGCR1WithMask(QN_SYSCON, mask&(~QN_MEM_RETENTION), MASK_ENABLE);

#if 0
    // gating all not needed modules
    mask = SYSCON_MASK_GATING_TIMER3
         | SYSCON_MASK_GATING_TIMER2
#if QN_32K_RCO == FALSE
         | SYSCON_MASK_GATING_TIMER1
#endif
         | SYSCON_MASK_GATING_TIMER0
         | SYSCON_MASK_GATING_UART1
         | SYSCON_MASK_GATING_UART0
         | SYSCON_MASK_GATING_SPI1
         | SYSCON_MASK_GATING_SPI0
         //| SYSCON_MASK_GATING_32K_CLK
         | SYSCON_MASK_GATING_SPI_AHB
         //| SYSCON_MASK_GATING_GPIO
         | SYSCON_MASK_GATING_ADC
         | SYSCON_MASK_GATING_DMA
         //| SYSCON_MASK_GATING_BLE_AHB
         | SYSCON_MASK_GATING_PWM
         ;
    syscon_SetCRSS(QN_SYSCON, mask&(~en));
#endif
}

/**
 ****************************************************************************************
 * @brief  Restore from low power mode
 * @param[in]    callback       callback before XTAL clock ready
 * @description
 *  This function is used to set MCU restoring from low power mode, switch system clock to XTAL.
 *****************************************************************************************
 */
void restore_from_low_power_mode(void (*callback)(void))
{
    syscon_SetPGCR2WithMask(QN_SYSCON, SYSCON_MASK_PD_STATE|SYSCON_MASK_DVDD12_PMU_SET, MASK_DISABLE);
    if (callback != NULL) {
        callback();
    }
    
    // 16MHz/32MHz XTAL is ready
    while (!(syscon_GetBLESR(QN_SYSCON) & SYSCON_MASK_CLK_RDY))
    {
        // XTAL shall be ready before BLE wakeup
        if (*(volatile uint32_t *)(0x2f000014) & 0x4)
        {
            // In this case XTAL wakeup duration is larger than setting.
            // The parameter 'Oscillator wake-up time' in the NVDS should be revised.
#if (QN_DBG_INFO)
            set_dbg_info(QN_DBG_INFO_XTAL_WAKEUP_DURATION);
#endif
        }
    }
    //syscon_SetCMDCRWithMask(QN_SYSCON, SYSCON_MASK_CLK_MUX, CLK_XTAL<<SYSCON_POS_CLK_MUX);
    syscon_set_sysclk_src(CLK_XTAL, __XTAL);
    syscon_set_ahb_clk(__AHB_CLK);
}


#endif /* CONFIG_ENABLE_DRIVER_SLEEP==TRUE */
/// @} SLEEP
